package org.jeecg.modules.wms.warehouse.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wms.config.WarehouseDictEnum;
import org.jeecg.modules.wms.warehouse.entity.WmsWarehouses;
import org.jeecg.modules.wms.warehouse.mapper.WmsWarehousesMapper;
import org.jeecg.modules.wms.warehouse.service.IWmsWarehousesService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 仓库表
 * @Author: jeecg-boot
 * @Date:   2025-09-02
 * @Version: V1.0
 */
@Service
public class WmsWarehousesServiceImpl extends ServiceImpl<WmsWarehousesMapper, WmsWarehouses> implements IWmsWarehousesService {

    @Override
    public void enable(String id) {
      //校验参数是否为空
        if(StringUtils.isEmpty(id)){
            throw new JeecgBootException("参数为空");
        }
        //检查仓库是否存在
        WmsWarehouses wmsWarehouses = this.getById(id);
        if(wmsWarehouses==null){
            throw new JeecgBootException("仓库不存在");
        }
        //只有状态为禁用或者创建
       if(!WarehouseDictEnum.STATUS_CREATED.getCode()
               .equals(wmsWarehouses.getStatus())&&!WarehouseDictEnum
               .STATUS_INACTIVE.getCode().equals(wmsWarehouses.getStatus())
       ){ throw new JeecgBootException("仓库状态为“创建”或禁用时方可启用");}
       wmsWarehouses.setStatus(WarehouseDictEnum.STATUS_ACTIVE.getCode());
       updateById(wmsWarehouses);

    }

    /**
     * 新增
     * @param wmsWarehouses
     */
    @Override
    public void add(WmsWarehouses wmsWarehouses) {
         //先判断仓库是否存在根据ID
        LambdaQueryWrapper<WmsWarehouses> eq = new LambdaQueryWrapper<WmsWarehouses>()
        .eq(WmsWarehouses::getWarehouseCode,wmsWarehouses.getWarehouseCode());
        if (this.count(eq)>0){
            throw new JeecgBootException("仓库已存在");
        }
        //初始状态为“创建”
        wmsWarehouses.setStatus(WarehouseDictEnum.STATUS_CREATED.getCode());
        save(wmsWarehouses);
    }
    /**
     * 修改
     */

    @Override
    public void edit(WmsWarehouses wmsWarehouses) {
        //修改时校验仓库代码是否已存在 sql select count(*) from wms_warehouses where warehouse_code = ? and id != ?
      LambdaQueryWrapper<WmsWarehouses> ne = new LambdaQueryWrapper<WmsWarehouses>()
              .eq(WmsWarehouses::getWarehouseCode,wmsWarehouses.getWarehouseCode())
              .ne(WmsWarehouses::getId,wmsWarehouses.getId());
      if (this.count(ne)>0){
          throw new JeecgBootException("仓库已存在");
      }
      updateById(wmsWarehouses);
    }

    /**
     * 禁用
     * @param id
     */

    @Override
    public void disable(String id) {
        //校验参数是否为空
        if(StringUtils.isEmpty(id)){
            throw new JeecgBootException("参数为空");
        }
        //检查仓库是否存在
        WmsWarehouses wmsWarehouses = this.getById(id);
        if(wmsWarehouses==null){
            throw new JeecgBootException("仓库不存在");
        }
       //只有状态为启用的时候才能够禁用
        if(!WarehouseDictEnum.STATUS_ACTIVE.getCode().equals(wmsWarehouses.getStatus())){
            throw new JeecgBootException("仓库状态为“启用”时方可禁用");
        }
        wmsWarehouses.setStatus(WarehouseDictEnum.STATUS_INACTIVE.getCode());
        updateById(wmsWarehouses);
    }

}
